using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for MembershipExpire
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class MembershipExpire : SqlServerDAO
    {
        #region MembershipExpire Private Fields

		private Int32 _id;
		private Int32 _userId;
		private Int32 _membershipId;
		private DateTime _expireDate;
		private Decimal _discount;

        #endregion

        #region MembershipExpire Constractors

		public MembershipExpire(){}

		public MembershipExpire
		(
			Int32 id,
			Int32 userId,
			Int32 membershipId,
			DateTime expireDate,
			Decimal discount

		)
		{
			this._id = id;
			this._userId = userId;
			this._membershipId = membershipId;
			this._expireDate = expireDate;
			this._discount = discount;
		}

        #endregion

        #region MembershipExpire Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public Int32 UserId
		{
			get{ return this._userId; }
			set{ this._userId = value; }
		}

		public Int32 MembershipId
		{
			get{ return this._membershipId; }
			set{ this._membershipId = value; }
		}

		public DateTime ExpireDate
		{
			get{ return this._expireDate; }
			set{ this._expireDate = value; }
		}

		public Decimal Discount
		{
			get{ return this._discount; }
			set{ this._discount = value; }
		}


        #endregion

        #region MembershipExpire Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewMembershipExpire
        (
			Int32 userId,
			Int32 membershipId,
			DateTime expireDate,
			Decimal discount

        )
        {
            //creating an empty MembershipExpire object
            MembershipExpire membershipExpire = new MembershipExpire();
            membershipExpire.UserId = userId;
			membershipExpire.MembershipId = membershipId;
			membershipExpire.ExpireDate = expireDate;
			membershipExpire.Discount = discount;

            return MembershipExpire.CreateNewMembershipExpire(membershipExpire);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateMembershipExpire
        (
			Int32 id,
			Int32 userId,
			Int32 membershipId,
			DateTime expireDate,
			Decimal discount

        )
        {
            MembershipExpire membershipExpire = MembershipExpire.GetMembershipExpireById(id);
            membershipExpire.UserId = userId;
			membershipExpire.MembershipId = membershipId;
			membershipExpire.ExpireDate = expireDate;
			membershipExpire.Discount = discount;

            return MembershipExpire.UpdateMembershipExpire(membershipExpire);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteMembershipExpire
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in MembershipExpire"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_DELETEMEMBERSHIPEXPIRE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteMembershipExpireList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in MembershipExpire"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_DELETEMEMBERSHIPEXPIRELIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<MembershipExpire> GetMembershipExpireByUserId(Int32 userId)
		{
			if(userId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("userId in MembershipExpire"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, userId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYUSERID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateMembershipExpireCollectionFromReader);
			CustomCollection<MembershipExpire> objCollection = ((CustomCollection<MembershipExpire>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<MembershipExpire> GetMembershipExpireByMembershipId(Int32 membershipId)
		{
			if(membershipId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("membershipId in MembershipExpire"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@MembershipId", SqlDbType.Int, 0, ParameterDirection.Input, membershipId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYMEMBERSHIPID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateMembershipExpireCollectionFromReader);
			CustomCollection<MembershipExpire> objCollection = ((CustomCollection<MembershipExpire>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

        #endregion

        #region MembershipExpire Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewMembershipExpire(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateMembershipExpire(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<MembershipExpire> GetAllMembershipExpire()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIRE);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipExpireCollectionFromReader);
            CustomCollection<MembershipExpire> objCollection = ((CustomCollection<MembershipExpire>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<MembershipExpire> GetAllMembershipExpirePaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIREPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipExpireCollectionFromReader);
            CustomCollection<MembershipExpire> objCollection = ((CustomCollection<MembershipExpire>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllMembershipExpirePagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIREPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static MembershipExpire GetMembershipExpireById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in MembershipExpire"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipExpireCollectionFromReader);
            CustomCollection<MembershipExpire> objCollection = ((CustomCollection<MembershipExpire>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (MembershipExpire)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewMembershipExpire(MembershipExpire newMembershipExpire)
        {
            // validate Parameters
            if (newMembershipExpire == null)
                throw (new ArgumentNullException("newMembershipExpire in MembershipExpire"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, newMembershipExpire.UserId);
			AddParameterToSqlCmd(sqlCmd, "@MembershipId", SqlDbType.Int, 0, ParameterDirection.Input, newMembershipExpire.MembershipId);
			AddParameterToSqlCmd(sqlCmd, "@ExpireDate", SqlDbType.DateTime, 0, ParameterDirection.Input, newMembershipExpire.ExpireDate);
			AddParameterToSqlCmd(sqlCmd, "@Discount", SqlDbType.Decimal, 0, ParameterDirection.Input, newMembershipExpire.Discount);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_CREATENEWMEMBERSHIPEXPIRE);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateMembershipExpire(MembershipExpire newMembershipExpire)
        {
            // validate Parameters
            if (newMembershipExpire == null)
                throw (new ArgumentNullException("newMembershipExpire"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newMembershipExpire.Id);
            AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, newMembershipExpire.UserId);
			AddParameterToSqlCmd(sqlCmd, "@MembershipId", SqlDbType.Int, 0, ParameterDirection.Input, newMembershipExpire.MembershipId);
			AddParameterToSqlCmd(sqlCmd, "@ExpireDate", SqlDbType.DateTime, 0, ParameterDirection.Input, newMembershipExpire.ExpireDate);
			AddParameterToSqlCmd(sqlCmd, "@Discount", SqlDbType.Decimal, 0, ParameterDirection.Input, newMembershipExpire.Discount);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIPEXPIRE_UPDATEMEMBERSHIPEXPIRE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region MembershipExpire Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of MembershipExpire custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// MembershipExpire information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateMembershipExpireCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of MembershipExpire collection
            CustomCollection<MembershipExpire> colMembershipExpire = new CustomCollection<MembershipExpire>();

            //Iterating thriugh the data reader, to generate MembershipExpire collection.

            //getting the MembershipExpire collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the MembershipExpire constructor parameters from the current instance of data reader fields.
                MembershipExpire newMembershipExpire = new MembershipExpire
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["user_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["user_id"],
					returnData["membership_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["membership_id"],
					returnData["expire_date"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["expire_date"],
					returnData["discount"] == System.DBNull.Value ? Decimal.MinValue : (Decimal)returnData["discount"]

                );
                //adding the MembershipExpire to the collection
                colMembershipExpire.Add(newMembershipExpire);
            }

            //returns the collection of MembershipExpire 
            return (colMembershipExpire);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_MEMBERSHIPEXPIRE_CREATENEWMEMBERSHIPEXPIRE = "spr_MembershipExpire_CreateNewMembershipExpire";
        internal const string SPR_MEMBERSHIPEXPIRE_UPDATEMEMBERSHIPEXPIRE = "spr_MembershipExpire_UpdateMembershipExpire";
        internal const string SPR_MEMBERSHIPEXPIRE_DELETEMEMBERSHIPEXPIRE = "spr_MembershipExpire_DeleteMembershipExpire";
        internal const string SPR_MEMBERSHIPEXPIRE_DELETEMEMBERSHIPEXPIRELIST = "spr_MembershipExpire_DeleteMembershipExpireList";
        internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYID = "spr_MembershipExpire_GetMembershipExpireById";
        internal const string SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIRE = "spr_MembershipExpire_GetAllMembershipExpire";
        internal const string SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIREPAGED = "spr_MembershipExpire_GetAllMembershipExpirePaged";
        internal const string SPR_MEMBERSHIPEXPIRE_GETALLMEMBERSHIPEXPIREPAGEDCOUNT = "spr_MembershipExpire_GetAllMembershipExpirePagedCount";

		//Foreign key contants
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYUSERID="spr_MembershipExpire_GetMembershipExpireByUserId";
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYUSERIDPAGED="spr_MembershipExpire_GetMembershipExpireByUserIdPaged";
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYUSERIDPAGEDCOUNT="spr_MembershipExpire_GetMembershipExpireByUserIdPagedCount";
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYMEMBERSHIPID="spr_MembershipExpire_GetMembershipExpireByMembershipId";
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYMEMBERSHIPIDPAGED="spr_MembershipExpire_GetMembershipExpireByMembershipIdPaged";
		internal const string SPR_MEMBERSHIPEXPIRE_GETMEMBERSHIPEXPIREBYMEMBERSHIPIDPAGEDCOUNT="spr_MembershipExpire_GetMembershipExpireByMembershipIdPagedCount";
		
        #endregion
    }
}
